package vue.panneau1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import modele.Equation2D;

public class PopupChoixLibelleContrainte extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private Interface2DZone1 zone2D;
	private Interface3DZone1 zone3D;
	
	private JDialog fen;
	private Box boxH1,boxH2,boxV;
	private JPanel pan;
	private JLabel label;
	private JTextField jtfLib;
	private JButton bOk,bAnnuler;
	private int index;
	private boolean is3D;

	public PopupChoixLibelleContrainte(Object interfaceZone1,int index,boolean is3D)
	{
		String libelle = "";
		if(is3D)
		{
			this.zone3D = (Interface3DZone1)interfaceZone1;
			libelle = zone3D.getTableModelEquation().getEquation3DAt(index).getLibelle();
		}
		else
		{
			this.zone2D = (Interface2DZone1)interfaceZone1;
			libelle = zone2D.getTableModelEquation().getEquation2DAt(index).getLibelle();
		}
		this.index = index;
		this.is3D = is3D;
		
		label = new JLabel("Contrainte n° " + (index+1) + " :");

		jtfLib = new JTextField(12);
		if(!libelle.equals(Equation2D.DEFAUT))
		{
			jtfLib.setText(libelle);
		}
		
		bOk = new JButton("OK");
		bAnnuler = new JButton("Annuler");
		
		bOk.setMaximumSize(bAnnuler.getMinimumSize());
		
		bOk.setActionCommand("ok");
		bAnnuler.setActionCommand("annuler");
		
		bOk.addActionListener(this);		
		bAnnuler.addActionListener(this);
		
		pan = new JPanel();
		pan.setLayout(new GridLayout(0,2));
		pan.add(label);
		pan.add(jtfLib);
		
		boxH1 = Box.createHorizontalBox();
		boxH1.add(Box.createHorizontalStrut(15));
		boxH1.add(pan);
		boxH1.add(Box.createHorizontalStrut(15));
		
		boxH2 = Box.createHorizontalBox();
		boxH2.add(Box.createHorizontalStrut(15));
		boxH2.add(bOk);
		boxH2.add(Box.createHorizontalStrut(15));
		boxH2.add(bAnnuler);
		boxH2.add(Box.createHorizontalStrut(15));
		
		boxV = Box.createVerticalBox();
		boxV.add(Box.createHorizontalStrut(50));
		boxV.add(boxH1);
		boxV.add(Box.createHorizontalStrut(30));
		boxV.add(boxH2);
		boxV.add(Box.createHorizontalStrut(50));
		
		this.setPreferredSize(new Dimension(250,75));
		this.setLayout(new BorderLayout());
		this.add(boxV,BorderLayout.CENTER);
		
		fen = new JDialog();
		fen.getRootPane().setDefaultButton(bOk);
		fen.add(this);
		fen.setTitle("Choix du libellé");
		
		if(is3D)
		{
			fen.setLocationRelativeTo(zone3D);
		}
		else
		{
			fen.setLocationRelativeTo(zone2D);
		}
		
		fen.setResizable(false);
		fen.setAlwaysOnTop(true);
		fen.setModal(true);
		fen.pack();
		fen.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{	
		String cmd = e.getActionCommand();
		
		if(cmd.equals("annuler"))
		{
			fen.dispose();
		}
		
		if(cmd.equals("ok"))
		{
			String lib = "";
			if(jtfLib.getText().equals(""))
			{
				lib = Equation2D.DEFAUT; 
			}
			else
			{
				lib = jtfLib.getText();
			}
			
			if(is3D)
			{
				zone3D.choisirLibelleContrainte(index,lib);
			}
			else
			{		
				zone2D.choisirLibelleContrainte(index,lib);
			}
			fen.dispose();
		}		
	}
}